package com.qf.confirm;

import com.qf.util.ConnUtil;
import com.rabbitmq.client.*;
import org.junit.Test;

import java.io.IOException;

public class Publisher {

    public static final String EXCHANGE_NAME = "topic";
    public static final String ORANGE_QUEUE = "orange-queue";

    @Test
    public void confirm1() throws Exception {
        //1. Conn
        Connection conn = ConnUtil.getConn();
        //2. Channel
        Channel channel = conn.createChannel();

        //3.1 创建Exchange
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC,true,false,null);
        //3.2 创建Queue1
        channel.queueDeclare(ORANGE_QUEUE,true,false,false,null);
        //3.3 queue1绑定到exchange
        channel.queueBind(ORANGE_QUEUE,EXCHANGE_NAME,"*.orange.*");

        //4. 开启Confirm
        channel.confirmSelect();

        //5. 发送消息到Exchange
        channel.basicPublish(EXCHANGE_NAME,"aaaaa",null,"到达交换机".getBytes());

        //6. 确定消息是否送达
        if (channel.waitForConfirms()) {
            System.out.println("消息成功的发送到了Exchange交换机...");
        }else{
            System.out.println("消息没有送到Exchange");
            //.....
        }

        //7. 释放资源
        channel.close();
        conn.close();




    }


    @Test
    public void confirm2() throws Exception {
        //1. Conn
        Connection conn = ConnUtil.getConn();
        //2. Channel
        Channel channel = conn.createChannel();

        //3.1 创建Exchange
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC,true,false,null);
        //3.2 创建Queue1
        channel.queueDeclare(ORANGE_QUEUE,true,false,false,null);
        //3.3 queue1绑定到exchange
        channel.queueBind(ORANGE_QUEUE,EXCHANGE_NAME,"*.orange.*");

        //4. 开启Confirm
        channel.confirmSelect();

        //5. 发送消息到Exchange
        channel.basicPublish(EXCHANGE_NAME,"aaaaa",null,"到达交换机".getBytes());

        //6. 确定消息是否送达
        try {
            channel.waitForConfirmsOrDie();
            System.out.println("消息已经送法");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            System.out.println("消息没有送到Exchange");
        }

        //7. 释放资源
        channel.close();
        conn.close();




    }


    @Test
    public void confirm3() throws Exception {
        //1. Conn
        Connection conn = ConnUtil.getConn();
        //2. Channel
        Channel channel = conn.createChannel();

        //3.1 创建Exchange
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC,true,false,null);
        //3.2 创建Queue1
        channel.queueDeclare(ORANGE_QUEUE,true,false,false,null);
        //3.3 queue1绑定到exchange
        channel.queueBind(ORANGE_QUEUE,EXCHANGE_NAME,"*.orange.*");

        //6. 确定消息是否送达
        channel.addConfirmListener(new ConfirmListener(){

            @Override
            public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("消息已经成功送达到Exchange");
            }

            @Override
            public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("消息已经没有送达到Exchange");
            }
        });

        //7. 查看消息是否路由到Queue中
        channel.addReturnListener(new ReturnListener() {
            @Override
            public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消息没有送达到queue中!!!" + exchange);
            }
        });

        //4. 开启Confirm
        channel.confirmSelect();

        //5. 发送消息到Exchange
        channel.basicPublish(EXCHANGE_NAME,"aaaaa.orange.xxxx",true,null,"到达交换机".getBytes());

        System.out.println("消息送达RabbitMQ,成功与否不知道!");



        System.in.read();

        //7. 释放资源
        channel.close();
        conn.close();




    }
}
